package com.gpframework.module.controller.process;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.process.domain.GpProcessCosting;
import com.gpframework.module.process.domain.req.GpProcessCostingAdd;
import com.gpframework.module.process.domain.req.GpProcessCostingReq;
import com.gpframework.module.process.domain.req.GpProcessCostingUpdate;
import com.gpframework.module.process.domain.vo.GpProcessCostingVo;
import com.gpframework.module.process.service.IGpProcessCostingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpProcessCostingController
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 成本核算Controller
 */
@Validated
@Api(tags = "成本核算")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/process/costing" )
public class GpProcessCostingController extends BaseController {

    private final IGpProcessCostingService iGpProcessCostingService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-20
  * @Param gpProcessCosting
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('process:costing:list')")
    @GetMapping("/list")
    public TableDataInfo<GpProcessCostingVo> list(GpProcessCostingReq gpProcessCostingReq)
    {
        startPage();
        QueryWrapper<GpProcessCosting> lqw = new QueryWrapper<GpProcessCosting>();
        gpProcessCostingReq.generatorQuery(lqw,true);
        List<GpProcessCostingVo> list = iGpProcessCostingService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('process:costing:add')" )
    @Log(title = "添加成本核算" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProcessCostingAdd gpProcessCostingAdd) {
        GpProcessCosting gpProcessCosting = Convert.convert(new TypeReference<GpProcessCosting>() {}, gpProcessCostingAdd);
        return toAjax(iGpProcessCostingService.save(gpProcessCosting) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('process:costing:edit')" )
    @Log(title = "修改成本核算" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpProcessCostingUpdate> list) {
//        GpProcessCosting gpProcessCosting = Convert.convert(new TypeReference<GpProcessCosting>() {}, gpProcessCostingUpdate);
        return toAjax(iGpProcessCostingService.edit(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('process:costing:export')" )
    @Log(title = "导出成本核算" , businessType = BusinessType.EXPORT)
    @ApiOperation("导出成本核算")
    @GetMapping("/export" )
    public AjaxResult export(GpProcessCostingReq gpProcessCostingReq) {
        QueryWrapper<GpProcessCosting> lqw = new QueryWrapper<GpProcessCosting>();
        gpProcessCostingReq.generatorQuery(lqw,true);
        List<GpProcessCostingVo> list = iGpProcessCostingService.listVo(lqw);
        ExcelUtil<GpProcessCostingVo> util = new ExcelUtil<GpProcessCostingVo>(GpProcessCostingVo. class);
        return util.exportExcel(list, "costing" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('process:costing:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProcessCostingVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProcessCostingService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('process:costing:remove')" )
    @Log(title = "删除成本核算" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProcessCostingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessCosting
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('process:costing:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpProcessCostingUpdate gpProcessCostingUpdate) {
        if (gpProcessCostingUpdate == null || gpProcessCostingUpdate.getId() == null || gpProcessCostingUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpProcessCostingUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpProcessCostingService.copy(gpProcessCostingUpdate.getId(), gpProcessCostingUpdate.getCopyNum()) ? 1 : 0);
    }
}
